package com.lego.servlet;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.lego.bean.Seller;
import com.lego.service.SellerService;
import com.lego.service.impl.SellerServiceImpl;

/**
 * Servlet implementation class AddSellerServlet
 */
@MultipartConfig
@WebServlet("/addSeller.do")
public class AddSellerServlet extends HttpServlet {
	SellerService ss = new SellerServiceImpl();
	@Override
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			String sellerName = request.getParameter("sellerName");
			String sellerPwd = request.getParameter("sellerPwd");
			String brandName = request.getParameter("brandName");
			Part part = request.getPart("sellerImg");
			String sellerImg = "";
			if(part != null && part.getSize() > 0){
				if(part.getContentType().contains("image")) {
					String fileName = UUID.randomUUID()+"_"+part.getSubmittedFileName();
					String savePath = "c:/legoImage/sellerImg/"+fileName;
					sellerImg = "/lego/sellerImg/"+fileName;
					part.write(savePath);
				}
			}
			Seller seller = ss.selectSeller(brandName,sellerName);
			if(seller!=null) {
				request.setAttribute("addSellerMsg", "添加失败");
				request.getRequestDispatcher("admin.jsp").forward(request, response);
				return;
			}
			System.out.println(brandName+"+"+sellerName+"+"+sellerPwd+"+"+sellerImg);
			int row = ss.addSeller(sellerName,sellerPwd,brandName,sellerImg);
			if(row!=1) {
				request.setAttribute("addSellerMsg", "添加失败");
				request.getRequestDispatcher("admin.jsp").forward(request, response);
				return;
			}
			response.sendRedirect("admin.jsp");
		}
	
}
